/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.skipass;

import java.util.Objects;

/**
 *
 * @author vlad
 */
public class PassByCount extends Skipass {
    
    private final PassType type;
    private int usesLeft;

    PassByCount(PassType type, int count, SkipassSystem system) {
        super(system);
        this.type = type;
        this.usesLeft = count;
    }

    public int getUsesLeft() {
        return usesLeft;
    }

    @Override
    public boolean validate() {
        if (this.getType() != system.getTodayPassType()) {
            return false;
        }
        return this.getUsesLeft() > 0;
    }

    @Override
    public void useCard() {
        if (!validate()) {
            system.incDeniedByCount(getType().ordinal());
            return;
        }
        if (isBlocked()) {
            system.incDeniedByCount(getType().ordinal());
            return;
        }
        system.incAllowedByCount(getType().ordinal());
        this.usesLeft--;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + Objects.hashCode(this.getType());
        hash = 89 * hash + this.getUsesLeft();
        hash = 89 * hash + Objects.hashCode(this.getUuid());
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        PassByCount other = (PassByCount) obj;
        if (!Objects.equals(this.getUuid(), other.getUuid())) {
            return false;
        }
        if (this.getType() != other.getType()) {
            return false;
        }
        return this.getUsesLeft() != other.getUsesLeft();
    }

    /**
     * @return the type
     */
    public PassType getType() {
        return type;
    }
    
}
